import 'package:flutter/material.dart';
import 'package:lx_widgets_library/lx_widgets_library.dart';

class BottomSheetWithList extends StatelessWidget {
  final String title;
  final List<LxBaseModel> items;
  final Function(int, bool)? onChanged;

  const BottomSheetWithList({
    Key? key,
    required this.title,
    required this.items,
    this.onChanged,
  }) : super(key: key);

  static Future<void> show({
    required BuildContext context,
    required String title,
    required List<LxBaseModel> items,
  }) {
    return showModalBottomSheet(
      context: context,
      isScrollControlled: true,
      builder: (context) {
        return BottomSheetWithList(title: title, items: items);
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      child: Padding(
        padding: EdgeInsets.only(
          bottom: MediaQuery.of(context).viewInsets.bottom,
        ),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            // 标题
            Padding(
              padding: const EdgeInsets.all(16.0),
              child: Text(
                title,
                style: Theme.of(context).textTheme.titleLarge,
              ),
            ),

            // 列表项
            ListView.separated(
              shrinkWrap: true,
              physics: const NeverScrollableScrollPhysics(),
              itemCount: items.length,
              separatorBuilder: (context, index) => const Divider(height: 1),
              itemBuilder: (context, index) {
                final item = items[index];
                return ListTile(
                  title: Text(item.bigTitle.value),
                  trailing: Switch(
                    value: item.status.value,
                    onChanged: (newValue) {
                      if (onChanged != null) {
                        onChanged!(index, newValue);
                      }
                    },
                  ),
                );
              },
            ),

            // 底部留白
            const SizedBox(height: 16),
          ],
        ),
      ),
    );
  }
}
